package com.qc31.monitorlib.ui.alarms

import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import autodispose2.autoDispose
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.kingja.loadsir.core.LoadService
import com.kingja.loadsir.core.LoadSir
import com.qc31.baselib.base.BaseRecycleActivity
import com.qc31.baselib.custom.MToolbar
import com.qc31.baselib.loadsie.EmptyCallback
import com.qc31.baselib.utils.*
import com.qc31.baselib.utils.ActivityResultUtil.launchAct
import com.qc31.baselib.utils.ActivityResultUtil.registerResultOk
import com.qc31.comlib.Keys
import com.qc31.comlib.entity.AlarmCount
import com.qc31.comlib.net.ServiceHelper
import com.qc31.monitorlib.R
import com.qc31.monitorlib.databinding.ActivityToolbarListBinding

/** @Author Jasper @Time 2020/11/30 @Desc 实时报警 */
class RealAlarmActivity :
    BaseRecycleActivity<ActivityToolbarListBinding>(ActivityToolbarListBinding::inflate) {
    private var mAlarmInfos = ArrayList<AlarmCount>()
    private val mAdapter by lazy { AlarmAdapter() }
    private var loadService: LoadService<Any>? = null
    private var isShowAlarm = false

    override fun initView() {
        super.initView()
        isShowAlarm = CacheMMKV.decodeBoolean(Keys.SETTING_ALARM_SHOW)
        ToolbarHelper.setToolbar(
            this,
            R.string.title_monitor_real_alarm,
            R.string.title_action_subscribe,
            object : MToolbar.IToolbarCallback {
                override fun actionOnClick(action: TextView) {
                    if (isFastClick()) return
                    launcher.launchAct<SubAlarmActivity>(this@RealAlarmActivity) {
                        putParcelableArrayListExtra(Keys.INTENT_DATA, mAlarmInfos)
                    }
                }
            }
        )

        loadService = LoadSir.getDefault().register(mRecyclerVB.recyclerView)
        loadService?.showSuccess()

        mRecyclerVB.recyclerView.layoutManager = LinearLayoutManager(this)
        mRecyclerVB.recyclerView.adapter = mAdapter
        mAdapter.setOnItemClickListener { adapter, view, position ->
            if (isFastClick()) return@setOnItemClickListener
            val info: AlarmCount = alarmList[position]
            if (info.alarmNum == 0) {
                showLongToast(R.string.toast_no_about_alarm_)
            } else {
                startActivityTo<AlarmInfoActivity>(this) {
                    putExtra(Keys.INTENT_TITLE, "${info.alarmName}(${info.alarmNum})")
                    putExtra(Keys.INTENT_ALARM_ID, info.alarmType)
                }
            }
        }
        getAlarmData()

    }

    override fun setListener() {

    }

    private var alarmList: ArrayList<AlarmCount> = ArrayList()

    private fun getAlarmData() {
        isShowLoading(true)
        ServiceHelper.getRepository().getAlarmCount().autoDispose(scopeProvider)
            .subscribe({ entity ->
                isShowLoading(false)
                mAlarmInfos.clear()
                mAlarmInfos.addAll(entity)

                if (entity.isNullOrEmpty()) {
                    loadService?.showCallback(EmptyCallback::class.java)
                    return@subscribe
                }
                alarmList.clear()
                alarmList.addAll(entity)
                if (isShowAlarm) {
                    entity.filter { it.alarmNum == 0 }.forEach { alarmList.remove(it) }
                }
                if (alarmList.isNullOrEmpty()) {
                    loadService?.showCallback(EmptyCallback::class.java)
                    alarmList.clear()
                } else {
                    loadService?.showSuccess()
                    alarmList.sort()
                }
                mAdapter.setList(alarmList)
            }, {
                isShowLoading(false)
                toast(it.message.toString())
            })
    }

    private val launcher = registerResultOk {
        getAlarmData()
    }


    override fun onDestroy() {
        super.onDestroy()
        loadService = null
    }

    private inner class AlarmAdapter :
        BaseQuickAdapter<AlarmCount, BaseViewHolder>(R.layout.item_image_title_num) {
        override fun convert(holder: BaseViewHolder, item: AlarmCount) {
            item.run {
                holder.setText(R.id.tv_item_title, alarmName)
                    .setText(R.id.tv_item_num, alarmNum.toString())
                holder.getView<ImageView>(R.id.iv_item_image)
                    .loadStringDImgError(context, iconLink, R.mipmap.ic_alarm_dafult)
            }

        }

    }
}